import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: false,
        isFavorite: false, //默认先不点亮销量上下按钮
        isPrice: false, // 默认先不点亮价格上下按钮
        pitchId: 1, //默认显示销量
        condition_one: true, //默认销量可以点击
        condition_two: false, //默认价格不点击
        searchList: [], //搜索得到的数据
    },
    search: {
        //搜索数据
        page: 1,
        size: 10,
        goodName: null,
        classifyId: "",
        salesOrderType: 2,
        priceOrderType: "",
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},
    //输入框change事件
    onChange(e) {
        console.log(e.detail);
        this.search.goodName = e.detail;
    },
    async searchList() {
        let res = await request({ url: "Goods/findUserAll", data: this.search });
        console.log(res);
        // 获取 总条数
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.search.size);
        if (res.data.records.length != 0) {
            this.setData({
                active: true,
            });
        } else {
            wx.showToast({
                title: "暂无商品",
                icon: "none",
            });
            this.setData({
                active: false,
            });
        }
        if (res.code == 200) {
            this.setData({
                searchList: [...this.data.searchList, ...res.data.records],
            });
        }
    },
    // 搜索按钮
    async onClick() {
        this.setData({
            searchList: [],
        });
        this.search.page = 1;
        this.searchList();
    },
    // 销量
    pitchTap(e) {
        console.log(e);
        this.setData({
            pitchId: e.target.dataset.pitchid,
        });
        if (this.data.pitchId == "1") {
            console.log("等于1");
            this.setData({
                condition_one: true,
                condition_two: false,
            });
            this.collect();
        } else if (this.data.pitchId == "2") {
            console.log("等于2");
            this.setData({
                condition_one: false,
                condition_two: true,
            });
            this.fluctuation();
        }
    },

    // 销量上下按钮
    async collect(e) {
        console.log(e);
        var that = this;
        var isFavorite = that.data.isFavorite;
        //上
        if (isFavorite == false) {
            that.setData({
                isFavorite: true,
            });
            this.search.salesOrderType = 2;
            this.search.priceOrderType = "";
            this.search.page = 1;
            let res = await request({ url: "Goods/findUserAll", data: this.search });
            console.log(res, "上");
            if (res.code == 200) {
                this.setData({
                    searchList: res.data.records,
                });
            }
        }
        //下
        else if (isFavorite == true) {
            that.setData({
                isFavorite: false,
            });
            this.search.salesOrderType = 1;
            this.search.priceOrderType = "";
            this.search.page = 1;

            let res = await request({ url: "Goods/findUserAll", data: this.search });
            console.log(res, "下");
            if (res.code == 200) {
                this.setData({
                    searchList: res.data.records,
                });
            }
        }
    },
    // 价格上下按钮
    async fluctuation(e) {
        console.log(e);
        var that = this;
        var isPrice = that.data.isPrice;
        //上
        if (isPrice == false) {
            that.setData({
                isPrice: true,
            });
            this.search.salesOrderType = "";
            this.search.priceOrderType = 1;
            this.search.page = 1;

            let res = await request({ url: "Goods/findUserAll", data: this.search });
            console.log(res, "下");
            if (res.code == 200) {
                this.setData({
                    searchList: res.data.records,
                });
            }
        }
        //下
        else if (isPrice == true) {
            that.setData({
                isPrice: false,
            });
            this.search.salesOrderType = "";
            this.search.priceOrderType = 2;
            this.search.page = 1;

            let res = await request({ url: "Goods/findUserAll", data: this.search });
            console.log(res, "下");
            if (res.code == 200) {
                this.setData({
                    searchList: res.data.records,
                });
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        //  1 判断还有没有下一页数据
        if (this.search.page >= this.totalPages) {
            wx.showToast({
                title: "我是有底线的",
                icon: "none",
            });
        } else {
            this.search.page++;
            this.searchList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});